import { Form } from '@ant-design/compatible';
import '@ant-design/compatible/assets/index.css';
import {InfoCircleOutlined} from "@ant-design/icons";
import { Input, Modal, InputNumber, Upload, Button, Icon, Select, Radio, Tooltip} from 'antd';
import { formatMessage,FormattedMessage } from 'umi-plugin-react/locale';
import { FormComponentProps } from '@ant-design/compatible/es/form';
import React, { useState } from 'react';
import styles from "../index.less";

// 导入自定义组件1
import PreviewForm from '@/components/PreviewForm';
import * as BdHelper from '@/utils/BdHelper';
import * as DataHelper from '@/helpers/DataHelper';
import * as TsxHelper from '@/helpers/TsxHelper';

// 常量
const { TextArea } = Input;
const FormItem = Form.Item;

// 定义: 数据表单属性
interface DataFormProps extends FormComponentProps {
  dataModalVisible: boolean;
  isUpdate: boolean
  dataFormValues: any
  common: any
  onSubmit: (fieldsValue: any) => any;
  onCancel: () => void;
}

// 数据表单
const DataForm: React.FC<DataFormProps> = props => {
  // 组件属性
  const { dataModalVisible, form, onSubmit: handleSubmit, onCancel,
    common, isUpdate,dataFormValues } = props;

  // 图片状态
  const [picture, setPicture] = useState<any[]>([]);
  const [uploadChanged, setUploadChanged] = useState<boolean>(false);
  const [previewModelVisible, handlePreviewModalVisible] = useState<boolean>(false);
  const [previewImg, setPreviewImg] = useState('');

  //提交表单
  const okHandle = () => {
    form.validateFields((err, fieldsValue) => {
      if (err) return;
      if(picture.length > 0){
        fieldsValue.file_id   = picture[0].file_id
        fieldsValue.file_url  = picture[0].file_url
      }
      // 如果是更新
      if(isUpdate){
        fieldsValue.id = dataFormValues.id;
      }
      console.log("submit fields ", fieldsValue)

      // 如果更新成功之后，重置表单
      handleSubmit(fieldsValue).then((success:any) => {
        console.log('handleSubmit then success is ', success)
        if(success){
          form.resetFields()
          setPicture([])
        }
      })

    });
  };

  // 设置上传文件值
  const setUploadFile = (fileList:any) => {
    setUploadChanged(true)
    let fileArr = DataHelper.getLastUploadFile(fileList)
    console.log("fileArr is ", fileArr)
    if(fileArr.length > 0){
      setPicture(fileArr)
      setPreviewImg(fileArr[0].file_url)
    }else if(fileArr.length === 0){
      console.log('set picture []')
      setPicture([])
    }
  }

  // 初始化变量
  const token = BdHelper.getBdToken()

  // 编辑时初始化 picture
  const defaultPicture = DataHelper.buildFileByUrl(dataFormValues.file_url)

  // 是否展示图片上传按钮
  const showUploadButton = DataHelper.showUploadBtn(isUpdate, picture, defaultPicture, uploadChanged)

  // 弹出框
  // @ts-ignore
  return <Modal
    destroyOnClose
    title={formatMessage({ id: 'app.base.create' })}
    width={DataHelper.DEFAULT_MODAL_WIDTH}
    visible={dataModalVisible}
    onOk={okHandle}
    onCancel={() => onCancel()}
  >
    <FormItem labelCol={{ span: 6 }} wrapperCol={{ span: 18 }} label={'过滤词'}>
      {form.getFieldDecorator('word', {
        initialValue: isUpdate ? dataFormValues.word : '',
        // rules: [{ required: true, message: formatMessage({ id: 'app.base.please_input_min_1' }) , min: 1 }],
      })(<Input placeholder={formatMessage({ id: 'app.base.please_input' })}/>)}
    </FormItem>

    <FormItem labelCol={{ span: 6 }} wrapperCol={{ span: 18 }} 
    label={
      <span>
        <FormattedMessage id="内容处理方式" />
        <em className={styles.optional}>
          <Tooltip title={<FormattedMessage id="帖子和评论" />}>
            <InfoCircleOutlined style={{ marginRight: 4,marginLeft: 4 }} />
          </Tooltip>
        </em>
      </span>
    }>
      {form.getFieldDecorator('content_mode', {
        initialValue: isUpdate ? (dataFormValues.content_mode).toString() : undefined,
        //  rules: [{ required: true, message: '请选择！',}],
      })(<Select placeholder={formatMessage({ id: 'app.base.please_select' })} size={'default'} style={{ width: '100%' }}>
        {common.contentModeOption && TsxHelper.buildSelectOption(common.contentModeOption)}
      </Select>)}
    </FormItem>

    <FormItem labelCol={{ span: 6 }} wrapperCol={{ span: 18 }} 
    label={
      <span>
        <FormattedMessage id="用户处理方式" />
        <em className={styles.optional}>
          <Tooltip title={<FormattedMessage id="用户名和签名" />}>
            <InfoCircleOutlined style={{ marginRight: 4,marginLeft: 4 }} />
          </Tooltip>
        </em>
      </span>
    }>
      {form.getFieldDecorator('member_mode', {
        initialValue: isUpdate ? (dataFormValues.member_mode).toString() : undefined,
        //  rules: [{ required: true, message: '请选择！',}],
      })(<Select placeholder={formatMessage({ id: 'app.base.please_select' })} size={'default'} style={{ width: '100%' }}>
        {common.memberModeOption && TsxHelper.buildSelectOption(common.memberModeOption)}
      </Select>)}
    </FormItem>
    <FormItem labelCol={{ span: 6 }} wrapperCol={{ span: 18 }} 
    label={
      <span>
        <FormattedMessage id="消息处理方式" />
        <em className={styles.optional}>
          <Tooltip title={<FormattedMessage id="会话私信" />}>
            <InfoCircleOutlined style={{ marginRight: 4,marginLeft: 4 }} />
          </Tooltip>
        </em>
      </span>
    }>
      {form.getFieldDecorator('dialog_mode', {
        initialValue: isUpdate ? (dataFormValues.dialog_mode).toString() : undefined,
        //  rules: [{ required: true, message: '请选择！',}],
      })(<Select placeholder={formatMessage({ id: 'app.base.please_select' })} size={'default'} style={{ width: '100%' }}>
        {common.memberModeOption && TsxHelper.buildSelectOption(common.memberModeOption)}
      </Select>)}
    </FormItem>
    <FormItem labelCol={{ span: 6 }} wrapperCol={{ span: 18 }} label={'过滤词替换'}>
      {form.getFieldDecorator('replace_word', {
        initialValue: isUpdate ? dataFormValues.replace_word : '',
        // rules: [{ required: true, message: formatMessage({ id: 'app.base.please_input_min_1' }) , min: 1 }],
      })(<Input placeholder={formatMessage({ id: 'app.base.please_input' })}/>)}
    </FormItem>
    {
      previewModelVisible && (
        <PreviewForm
          previewModelVisible={previewModelVisible}
          onVisibleModel={handlePreviewModalVisible}
          previewImg={previewImg}
        />
      )
    }
  </Modal>;
};

export default Form.create<DataFormProps>()(DataForm);
